<?php

namespace App\Console\Commands;
use App\Imgur;
use Log;
use Illuminate\Console\Command;

class ImgurFetcher extends Command
{
    const ClientID = 'eed90f706e965db';
    const ClientSecret = '';//搞忘了 现在还不需要
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'spider:imgurImages';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '爬去imgur的图片';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {

        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(1000);

        $this->gallery();
    }

    //https://apidocs.imgur.com/#f06b345c-ce1b-1c91-2a1c-ab1a6cc6f911
    public function gallery()
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.imgur.com/3/gallery/hot/viral",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => array(
                "authorization: Client-ID eed90f706e965db"
            ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);

        if ($err) {
            $message = "爬去imgur事变 #:" . $err;
            Log::alert($message);
            echo $message;
        } else {
            $obj = json_decode($response);
            dd($obj->data);

            foreach ($obj->data as $item){
                echo  $item->title."\n";

                Imgur::insertSpiderJson($item);
            }


        }
    }
    //https://apidocs.imgur.com/#05702d3c-d30e-b2c8-5e4f-39ecc0fea957
    public function subRedit($obj){

    }



}
